package com.sumainfor.pc.epc.service.impl;

import cn.afterturn.easypoi.entity.vo.MapExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.sumainfor.common.easypoiExcel.utils.ExcelStyleUtil;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.epc.dao.RepairExcMapper;
import com.sumainfor.pc.epc.feign.GmioFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Meng
 * @ClassName RepairExcServiceImpl
 * @date 2020/2/11 14:24
 * @description: TODO
 */

@Service
public class RepairExcServiceImpl {


    @Autowired
    RepairExcMapper repairExcMapper;

    @Autowired
    GmioFeign gmioFeign;

    @Autowired
    ZpcMktFeign zpcMktFeign;

    @Autowired
    ZzuulFeign zzuulFeign;

    public JsonResult getRepairList(Map<String,Object> params, Pager pager){
        JsonResult result = new JsonResult();

        pager.setPageno((Integer) params.get("pageno"));
        pager.setPagesize((Integer) params.get("pagesize"));
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();

        List<String> HouList = new ArrayList<>();
            List<Map<String,Object>> MktHouList = zpcMktFeign.getHoulist(params);
            if (MktHouList.size()>0){
                for (Map<String,Object> map1:MktHouList){
                    HouList.add(map1.get("HouId").toString());
                }
            }

        if (HouList.size()>0){
            params.put("HouList",HouList);
        }

        Integer cout =  repairExcMapper.getRepairCount(params);
        if(cout == 0){
            return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(),params,cout);
        }else {
            List<Map<String, Object>> getRepairList = repairExcMapper.getRepairList(params);
            if (getRepairList.size()>0){

                for (Map<String,Object> getRepairMap: getRepairList) {

                    if (MapUtils.getString(getRepairMap,"OrdBiId")!=null&&!MapUtils.getString(getRepairMap,"OrdBiId").equals("")){
                        getRepairMap.put("BiId",getRepairMap.get("OrdBiId"));
                        JsonResult gmioResult = gmioFeign.getRepairBillMap(getRepairMap);
                        Map<String,Object> gmioMap = (Map<String, Object>) gmioResult.get("result");
                        getRepairMap.putAll(gmioMap);
                    }

                    if (MapUtils.getString(getRepairMap,"SysUserId")!=null&&!MapUtils.getString(getRepairMap,"SysUserId").equals("")){
                        getRepairMap.put("user_id",getRepairMap.get("SysUserId"));
                        JsonResult zuulResult = zzuulFeign.getLoginName(getRepairMap);
                        Map<String,Object> zuulMap = (Map<String, Object>) zuulResult.get("result");
                        getRepairMap.putAll(zuulMap);
                    }


                    if (getRepairMap.get("HouCon").toString()!=null&&!getRepairMap.get("HouCon").toString().equals("")){

                        Map<String,Object> mktMap = zpcMktFeign.getHouEpc(getRepairMap);
                        getRepairMap.putAll(mktMap);
                    }


                }


            }

            return pageUtils.getJsonResult(getRepairList,params,cout);
        }

    }

    /**
     * 导出维修统计表
     *
     */

    public ModelMap exportRepairList(ModelMap modelMap, Map<String, Object> params) throws Exception {


        //获取维修数据
        List<Map<String, Object>> getRepairList = repairExcMapper.getRepairList(params);
        if (getRepairList.size()>0){

            for (Map<String,Object> getRepairMap: getRepairList) {

                if (MapUtils.getString(getRepairMap,"OrdBiId")!=null&&!MapUtils.getString(getRepairMap,"OrdBiId").equals("")){
                    getRepairMap.put("BiId",getRepairMap.get("OrdBiId"));
                    JsonResult gmioResult = gmioFeign.getRepairBillMap(getRepairMap);
                    Map<String,Object> gmioMap = (Map<String, Object>) gmioResult.get("result");
                    getRepairMap.putAll(gmioMap);
                }

                if (MapUtils.getString(getRepairMap,"SysUserId")!=null&&!MapUtils.getString(getRepairMap,"SysUserId").equals("")){
                    getRepairMap.put("user_id",getRepairMap.get("SysUserId"));
                    JsonResult zuulResult = zzuulFeign.getLoginName(getRepairMap);
                    Map<String,Object> zuulMap = (Map<String, Object>) zuulResult.get("result");
                    getRepairMap.putAll(zuulMap);
                }


                if (getRepairMap.get("HouCon").toString()!=null&&!getRepairMap.get("HouCon").toString().equals("")){

                    Map<String,Object> mktMap = zpcMktFeign.getHouEpc(getRepairMap);
                    getRepairMap.putAll(mktMap);
                }

            }

        }





        List<ExcelExportEntity> entityList = new ArrayList<>();
        //构建表格
        ExportParams exportParams = new ExportParams(String.format("维修统计表%s~%s"), "维修统计", ExcelType.XSSF);
        exportParams.setStyle(ExcelStyleUtil.class);
        exportParams.setFixedTitle(true);

        // entityList.add(new ExcelExportEntity("收款日期", "BiGenerated"));
//        entityList.add(new ExcelExportEntity("公司", "name"));
//        entityList.add(new ExcelExportEntity("服务中心", "deptname"));
        entityList.add(new ExcelExportEntity("所属项目", "ComName"));
        entityList.add(new ExcelExportEntity("楼号", "BuilDing"));
        entityList.add(new ExcelExportEntity("房号", "Room"));
        entityList.add(new ExcelExportEntity("房间号", "cod"));
        //entityList.add(new ExcelExportEntity("收款方式", "payType"));

        ExcelExportEntity deposit = new ExcelExportEntity("报修来源", "RepTypeName");
        deposit.setStatistics(true);
        entityList.add(deposit);

        ExcelExportEntity el = new ExcelExportEntity("报修时间", "instm");
        el.setStatistics(true);
        entityList.add(el);

        ExcelExportEntity water = new ExcelExportEntity("接单人", "login_name");
        water.setStatistics(true);
        entityList.add(water);

        ExcelExportEntity gas = new ExcelExportEntity("是否强排", "RepStateOr");
        gas.setStatistics(true);
        entityList.add(gas);

        ExcelExportEntity net = new ExcelExportEntity("报修内容", "TroRemark");
        net.setStatistics(true);
        entityList.add(net);


        ExcelExportEntity weixiu = new ExcelExportEntity("是否完成", "OrdStateOr");
        weixiu.setStatistics(true);
        entityList.add(weixiu);

        ExcelExportEntity linshi = new ExcelExportEntity("是否更换物品", "linshiMoney");//
        linshi.setStatistics(true);
        entityList.add(linshi);

        ExcelExportEntity qite = new ExcelExportEntity("产生维修费用", "CostPrice");
        qite.setStatistics(true);
        entityList.add(qite);

        ExcelExportEntity tuizu = new ExcelExportEntity("支付状态", "Status");//
        tuizu.setStatistics(true);
        entityList.add(tuizu);

        ExcelExportEntity yejian = new ExcelExportEntity("支付日期", "PayTime");
        yejian.setStatistics(true);
        entityList.add(yejian);

//        ExcelExportEntity zhineng = new ExcelExportEntity("智能电表", "zhinengMoney");
//        zhineng.setStatistics(true);
//        entityList.add(zhineng);
//
//
//
//        ExcelExportEntity allSum = new ExcelExportEntity("收款合计", "allSum");
//        allSum.setStatistics(true);
//        entityList.add(allSum);

        for (ExcelExportEntity entity : entityList) {
            entity.setWidth(15);
        }

        modelMap.put(MapExcelConstants.MAP_LIST, getRepairList);
        modelMap.put(MapExcelConstants.ENTITY_LIST, entityList);
        modelMap.put(MapExcelConstants.PARAMS, exportParams);
        modelMap.put(MapExcelConstants.FILE_NAME, String.format("杂费收款统计报表 (%s~%s)"));
        return modelMap;


    }
}
